package kr.co.redcircle.loader.data;

import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

public class CryptoTestCase {

	/**
	 * @param args
	 * @throws NoSuchPaddingException 
	 * @throws NoSuchProviderException 
	 * @throws NoSuchAlgorithmException 
	 * @throws InvalidKeyException 
	 * @throws ShortBufferException 
	 * @throws BadPaddingException 
	 * @throws IllegalBlockSizeException 
	 * @throws InvalidAlgorithmParameterException 
	 * @throws IOException 
	 */
	public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, ShortBufferException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, IOException {
		Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); 
		byte[] input = (((Math.random() * 1000d) + "").substring(0, 3)  + 
				"|" + "11111www.java2s.com  asdfasdf  asdfasdfasdfasdfasdfasdf" +
						" eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" +
						"99999999999999999999999999999999999999999" +
						"99999999999999999999999999999999999999999" +
						"999").getBytes();

		byte[] keyBytes = new byte[] { 
				0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
				0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
				}; 
		
		byte[] IV = new byte[] { 
				0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x01, 
				0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 }; 
		
		SecretKeySpec key = new SecretKeySpec(keyBytes, "SEED");
		
		IvParameterSpec ivSpec = new IvParameterSpec(keyBytes); 
		Cipher cipher = Cipher.getInstance("SEED/CBC/PKCS5Padding", "BC"); 

		System.out.println("input text : " + new String(input));

		// encryption pass
		cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec); 
		byte[] cipherText = cipher.doFinal(input); 	
		String cipherTextString = Base64.encodeBase64String(cipherText);
		System.out.print(cipherTextString);

		// decryption pass
		byte[] cipherText2 = Base64.decodeBase64(cipherTextString);
		cipher.init(Cipher.DECRYPT_MODE, key, ivSpec); 
		String plainText = new String(cipher.doFinal(cipherText2)); 
		System.out.println("plain text : " + plainText + " bytes: " + plainText.length());
	}

}
